package com.hc.accountservice.service.Impl;

import com.hc.accountservice.mapper.AccountMapper;
import com.hc.accountservice.service.AccountService;
import com.hc.entity.Order;
import com.hc.entity.PayRecord;
import com.hc.entity.Wallet;
import com.hc.entity.WalletOpr;
import com.hc.utils.UtilDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @BelongsProject: hc
 * @BelongsPackage: com.hc.accountservice.service.Impl
 * @Author: WY
 * @CreateTime: 2022-10-19  19:49
 * @Descriprion: TODO
 * @Version: 1.0
 **/
@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    //查询支付记录
    @Override
    public List<PayRecord> findByPage(PayRecord payRecord) {
        List<PayRecord> payRecordList = accountMapper.findByPage(payRecord);
        return payRecordList;
    }
    //查询余额
    @Override
    public Wallet findBalance(int id) {
        Wallet wallet = accountMapper.findBalance(id);
        return wallet;
    }

    //钱包充值与记录充值信息
    @Override
    public void topUp(Wallet wallet) {
        String way = wallet.getWay();
        BigDecimal addmoney = wallet.getAddmoney();
        wallet = accountMapper.findBalance(wallet.getUserId());
        BigDecimal totalmoney = addmoney.add(wallet.getBalance());
        wallet.setBalance(totalmoney);
        accountMapper.topUp(wallet);
        WalletOpr walletOpr = new WalletOpr();
        walletOpr.setWay(way);
        walletOpr.setWalletId(wallet.getId());
        walletOpr.setDateTime(UtilDateTime.getDateTime());
        accountMapper.moneyLog(walletOpr);
    }

    //订单退款
    @Override
    public void orderRefund(PayRecord payRecord) {
        payRecord = accountMapper.findById(payRecord.getId());
        accountMapper.del(payRecord.getId());
        accountMapper.updatestatus(payRecord.getOrder().getId());
        Wallet wallet = accountMapper.findBalance(payRecord.getUserId());
        BigDecimal totalmoney = payRecord.getFee().add(wallet.getBalance());
        wallet.setBalance(totalmoney);
        accountMapper.topUp(wallet);
        WalletOpr walletOpr = new WalletOpr();
        walletOpr.setDateTime(UtilDateTime.getDateTime());
        walletOpr.setWalletId(wallet.getId());
        walletOpr.setWay(payRecord.getWay());
        walletOpr.setOrderId(payRecord.getOrder().getId());
        accountMapper.moneyLog(walletOpr);
    }
}
